package com.cv4j.app.fragment;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseFragment;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.FloatProcessor;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.datamodel.Point;
import com.cv4j.core.tpl.TemplateMatch;
import com.cv4j.image.util.Tools;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

/**
 * TemplateMatchFragment
 *
 * @since 2021-05-14
 */
public class TemplateMatchFragment extends BaseFragment {
    private Image image;
    private Image templateimage;
    private Image templateresult;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component root = scatter.parse(ResourceTable.Layout_fragment_template_match, container, false);
        initView(root);
        initData();
        return root;
    }

    private void initData() {
        image.setPixelMap(ResourceTable.Media_test_tpl_target);
        templateimage.setPixelMap(ResourceTable.Media_tpl);
        CV4JImage targetcv4j = new CV4JImage(image.getPixelMap());
        ImageProcessor targetImageProcessor = targetcv4j.convert2Gray().getProcessor();
        CV4JImage templatecv4j = new CV4JImage(templateimage.getPixelMap());
        ImageProcessor templateProcessor = templatecv4j.convert2Gray().getProcessor();
        TemplateMatch match = new TemplateMatch();
        FloatProcessor floatProcessor = match.match(targetImageProcessor,
            templateProcessor, TemplateMatch.TM_CCORR_NORMED);
        Point[] points = Tools.getMinMaxLoc(floatProcessor.getGray(),
            floatProcessor.getWidth(), floatProcessor.getHeight());
        Point resultPoint = null;
        if (points != null) {
            resultPoint = points[0];

            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.size = new Size(image.getWidth(), image.getHeight());
            options.pixelFormat = PixelFormat.ARGB_8888;
            PixelMap pixelMap = PixelMap.create(image.getPixelMap(), options);
            Texture texture = new Texture(pixelMap);
            Canvas canvas = new Canvas(texture);
            PixelMapElement element = new PixelMapElement(pixelMap);
            element.drawToCanvas(canvas);
            final int count = 2;
            int sx = resultPoint.x1 + templateProcessor.getWidth() / count;
            int sy = resultPoint.y1 + templateProcessor.getHeight() / count;
            final RectFloat rect = new RectFloat(sx, sy,
                sx + templateProcessor.getWidth(), sy + templateProcessor.getHeight());

            final float dink = (float) 4.0;
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.STROKE_STYLE);
            paint.setStrokeWidth(dink);
            paint.setColor(Color.RED);
            canvas.drawRect(rect, paint);
            templateresult.setImageElement(element);
        }
    }

    private void initView(Component root) {
        image = (Image) root.findComponentById(ResourceTable.Id_target_image);
        templateimage = (Image) root.findComponentById(ResourceTable.Id_template_image);
        templateresult = (Image) root.findComponentById(ResourceTable.Id_template_result);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }
}
